package com.bw.a1908.fx;

import android.Manifest;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Point;
import android.hardware.Camera;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.bw.a1908.R;
import com.bw.a1908.core.mvp.BaseActivity;
import com.bw.a1908.core.mvp.IPresenter;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import top.littlefogcat.danmakulib.danmaku.Danmaku;
import top.littlefogcat.danmakulib.danmaku.DanmakuManager;

public class FXActivity extends BaseActivity<IPresenter> implements FXContract.View, View.OnClickListener {

    private StandardGSYVideoPlayer videoPlayer;
    private FrameLayout dmLayout;
    private LinearLayout giftLayout;
    private FrameLayout goodLayout;

    @Override
    public int bandLayout() {
        return R.layout.act_fx;
    }

    @Override
    public void initView() {
        videoPlayer = findViewById(R.id.act_fx_video_player);
        dmLayout = findViewById(R.id.act_fx_dm_layout);
        giftLayout = findViewById(R.id.act_fx_gift_layout);
        goodLayout = findViewById(R.id.act_fx_good_layout);
        findViewById(R.id.act_fx_send_dm).setOnClickListener(this);
        findViewById(R.id.act_fx_send_gift).setOnClickListener(this);
    }

    @Override
    public void initData() {
        DanmakuManager.getInstance().init(FXActivity.this, dmLayout);
        openLive("http://txmov2.a.yximgs.com/upic/2020/08/28/18/BMjAyMDA4MjgxODMzMzRfNDcwNDMxNDkzXzM1MTY5NTMzNTA2XzFfMw==_b_B59623929ea2c73208950bf09dcbb1e24.mp4?tag=1-1599271127-vdg-1-0uohyia5ss-560527acb5e9ad7d&tt=b&bp=10000");

    }

    @Override
    public void openLive(String url) {
        videoPlayer.setUp(url, true, "");
        videoPlayer.startPlayLogic();
    }

    @Override
    public void sendText(String text) {
        Danmaku danmaku = new Danmaku();
        danmaku.text = text;
        danmaku.color = "#ffff0000";
        danmaku.size = 50;
        DanmakuManager.getInstance().send(danmaku);
    }

    @Override
    public void sendGift(int id) {
        ImageView giftView = new ImageView(this);
        giftView.setImageResource(id);
        giftView.setBackgroundColor(Color.parseColor("#55000000"));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(300, 80);
        giftView.setLayoutParams(params);

        //动画
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(giftView, "TranslationX", -300, 0);
        animator1.setDuration(1000);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(giftView, "Alpha", 1, 1);
        animator2.setDuration(2000);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(giftView, "Alpha", 1, 0);
        animator3.setDuration(1000);

        AnimatorSet set = new AnimatorSet();
        set.play(animator2).after(animator1).before(animator3);
        set.start();

        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                giftLayout.removeView(giftView);
            }
        });

        giftLayout.addView(giftView);
    }

    @Override
    public void clickGood(Point point) {
        ImageView hartIv = new ImageView(this);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(300, 300);
        params.leftMargin = point.x - 150;
        params.topMargin = point.y - 150;
        hartIv.setLayoutParams(params);
        hartIv.setImageResource(R.mipmap.cab);

        ObjectAnimator animatorX = ObjectAnimator.ofFloat(hartIv, "ScaleX", 1, 2);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(hartIv, "ScaleY", 1, 2);
        ObjectAnimator animatorA = ObjectAnimator.ofFloat(hartIv, "Alpha", 1, 0);
        AnimatorSet set = new AnimatorSet();
        set.setDuration(2000);
        set.play(animatorX).with(animatorY).with(animatorA);
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                goodLayout.removeView(hartIv);
            }
        });
        set.start();
        goodLayout.addView(hartIv);
    }

    @Override
    public void showAudienceList() {

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.act_fx_send_dm:
                sendText("漂亮小姐姐");
                break;
            case R.id.act_fx_send_gift:
                sendGift(R.mipmap.cab);
                break;
        }
    }

    private boolean isClick = false;

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            LogUtils.d("onTouchEvent ACTION_DOWN");
            //双击触发 固定时间内执行2次点击
            if (!isClick) {
                //没点过
                isClick = true;
                //开启还原方法
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (isClick){
                            try {
                                Thread.sleep(300);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        isClick = false;
                    }
                }).start();
            } else {
                isClick = false;
                clickGood(new Point((int) event.getX(), (int) event.getY()));
            }
        }
        return super.dispatchTouchEvent(event);
    }

    //需要考虑到获取不到动态权限的问题
    private void f1(){
        PermissionUtils.permission(Manifest.permission.CAMERA)
                .callback(new PermissionUtils.SimpleCallback() {
                    @Override
                    public void onGranted() {
                        //赋予权限
                    }

                    @Override
                    public void onDenied() {
                        //未赋予权限，用户手打拒绝
                        //Dialog 请打开摄像机权限 打开 取消
                        //打开 -> 跳转对应应用设置页面

                        startActivity(new Intent(Settings.ACTION_SETTINGS));
                    }
                })
                .request();
    }

    //需要考虑到因为网络的原因播放不流畅的问题
    private void f2(){
        //Dialog 当前网络环境差 切换移动网络 继续使用局域网
        //关闭WIFI 打开移动网络
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        //打开关闭WIFI
        wifiManager.setWifiEnabled(true);

        startActivity(new Intent(Settings.ACTION_SETTINGS));
    }

    //需要考虑到是否有缓存和回放的问题 ？？？？？
    private void f3(){

    }

    //需要考虑到弱网环境下画质自动切换问题
    private void f4(){
        //在接口超时时，提供对网络切换的 Dialog
        //访问不同的拉流地址，大家用一个
    }

    //需要考虑到弱网环境下画质自动切换问题
    private void f5(){
        //判断当前网络如果时移动网络 使用高清播放
        //如果时局域网 使用蓝光播放
        //是否时wifi
        NetworkUtils.isWifiConnected();
        NetworkUtils.is5G();
        NetworkUtils.is4G();
    }

    //需要考虑到主播的摄像头被占用的问题
    private void f6(){
        //开启直播 判断摄像头是否被占用
        isCameraCanUse();
    }

    public static boolean isCameraCanUse() {
        boolean canUse = false;
        Camera mCamera = null;
        try {
            mCamera = Camera.open(0);
        } catch (Exception e) {
            canUse = false;
        }
        if (mCamera != null) {
            mCamera.release();
            canUse = true;
        }
        return canUse;
    }

    //需要考虑到框架中内存泄露的问题
    //需要考虑到rxjava的内存泄漏问题
    //RxJava Activity销毁对呀观察者解除订阅关系


    //需要考虑到TA的动态中的大图可能造成的泄漏问题
    //Glide 已经实现了
}
